package com.asset.dao;
/*
 *@ClassName AdminDao
 *@Author B21015118吴逸鑫
 *@Date 2022/10/8 22:57
 */
import com.asset.bean.User;
import com.asset.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.lang.*;
import static java.lang.System.out;
public class AdminDAO {
    public User login(String username, String password,String level){
        User a = null;
        Connection con = null;
        Statement sta = null;
        ResultSet res = null;
        try {
            //1.获取连接
            con = JDBCUtils.getConnection();
            //2.定义sql
            String sql = "select * from user where (no = '"+username+"' or mail ='"+username+"') and pwd = '"+password+"' and level='1'";
            //3.获取执行sql的对象
            sta = con.createStatement();
            //4.执行查询
            res = sta.executeQuery(sql);
            //5.判断
            if(res.next()){
                a = new User();
                a.setName(res.getString("name"));
                a.setPwd(res.getString("password"));
                a.setLevel(res.getString("level"));
                out.println("登录成功");
            } else{
                out.println("等级过低或用户名、密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res, sta, con);
        }
        return a;
    }

    /**
     * 注册
     */

    /**
    public boolean register(User user){
        String username=  user.getName();
//
        int id;
        String level=user.getLevel();
        String age= user.getAge();
        String mail = user.getMail();
        String password = user.getPwd();
        String no=  user.getNo();
        String phone = user.getPhone();

//        String subject = user.getSubject();
        Connection con = null;
        Statement sta = null;
        ResultSet res = null;
        try {
            con = JDBCUtils.getConnection();
            sta = con.createStatement();

            //判断输入是否合法
            if(username.length()==0 || password.length()==0){
                return false;
            }

            //判断用户名是否已存在
            String querySql = "select * from user where no = '" + no + "'";
            res = sta.executeQuery(querySql);
            if (res.next()) {
                out.println("工号已存在！");
                return false;
            }

            System.out.println(username);

            //添加新用户
            String idSql = "select * from user";
            int n=0;
            res = sta.executeQuery(idSql);
            while(res.next()){
                ++n;
            }
            id=n+1;
            System.out.println(id);
            // String insertSql = "insert into user (no, phone, age, name,mail,pwd,level) values ('" + no + "','" + phone + "','" + age + "','" + username + "','" + mail + "','" + password + "','" + level + "')";
            //String insertSql ="INSERT INTO user( name, age, level, mail, pwd, phone, no) VALUES ('" + username + "','" + age + "','" + level + "','" + mail + "','" + password + "','" + phone + "'" + no + ")";
            String insertSql ="INSERT INTO `user` (`id`, `name`, `age`, `level`, `mail`, `pwd`, `phone`, `no`) VALUES ('" + id + "','" + username + "','" + age + "','" + level + "','" + mail + "','" + password + "','" + phone + "','" + no + "')";

            sta.executeUpdate(insertSql);


        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.close(res, sta, con);
        }
        return true;
    }
    **/


}
